package com.dhh.sky.model;

import org.json.JSONException;
import org.json.JSONObject;
import com.dhh.sky.R;
import com.dhh.sky.util.Constant;

public abstract class SkyDriveObject {
	private  String meta;
    public interface Visitor {
        public void visit(SkyDriveAlbum album);
        public void visit(SkyDriveAudio audio);
        public void visit(SkyDrivePhoto photo);
        public void visit(SkyDriveFolder folder);
        public void visit(SkyDriveFile file);
        public void visit(SkyDriveVideo video);
    }

    public static class From {
        private final JSONObject mFrom;

        public From(JSONObject from) {
            assert from != null;
            mFrom = from;
        }

        public String getName() {
            return mFrom.optString("name");
        }

        public String getId() {
            return mFrom.optString("id");
        }

        public JSONObject toJson() {
            return mFrom;
        }
    }

    public static class SharedWith {
        private final JSONObject mSharedWidth;

        public SharedWith(JSONObject sharedWith) {
            assert sharedWith != null;
            mSharedWidth = sharedWith;
        }

        public String getAccess() {
            return mSharedWidth.optString("access");
        }

        public JSONObject toJson() {
            return mSharedWidth;
        }
    }


    public static SkyDriveObject create(JSONObject skyDriveObject) {
        String type = skyDriveObject.optString("type");

        if (type.equals(SkyDriveFolder.TYPE)) {
            return new SkyDriveFolder(skyDriveObject);
        } else if (type.equals(SkyDriveAlbum.TYPE)) {
            return new SkyDriveAlbum(skyDriveObject);
        } else if (type.equals(SkyDrivePhoto.TYPE)) {
            return new SkyDrivePhoto(skyDriveObject);
        } else if (type.equals(SkyDriveVideo.TYPE)) {
            return new SkyDriveVideo(skyDriveObject);
        } else if (type.equals(SkyDriveAudio.TYPE)) {
            return new SkyDriveAudio(skyDriveObject);
        } else {
            return new SkyDriveFile(skyDriveObject);
        }
    }

    public final JSONObject mObject;

    public SkyDriveObject(JSONObject object) {
        assert object != null;
        mObject = object;
    }

    public abstract void accept(Visitor visitor);

    public String getId() {
        return mObject.optString("id");
    }

    public From getFrom() {
        return new From(mObject.optJSONObject("from"));
    }

    public String getName() {
        return mObject.optString("name");
    }

    public String getParentId() {
        return mObject.optString("parent_id");
    }

    public String getDescription() {
        return mObject.isNull("description") ? null : mObject.optString("description");
    }

    public String getType() {
        return mObject.optString("type");
    }

    public String getLink() {
        return mObject.optString("link");
    }

    public String getCreatedTime() {
        return mObject.optString("created_time");
    }

    public String getUpdatedTime() {
        return mObject.optString("updated_time");
    }

    public String getUploadLocation() {
        return mObject.optString("upload_location");
    }

    public SharedWith getSharedWith() {
        return new SharedWith(mObject.optJSONObject("shared_with"));
    }
    
    public String getMeta() {
		return meta;
	}

	public void setMeta(String meta) {
		this.meta = meta;
	}
	
	public void setParent(String parent){
    	try {
			mObject.remove("parent_id");
			mObject.put("parent_id", parent);
		} catch (JSONException e) {
		}
    }
	
	public void setName(String name){
    	try {
			mObject.put("name", name);
		} catch (JSONException e) {
		}
    }
    
    public int getIcon(){
    	int icon = R.drawable.ic_folder_blue;
    	if(SkyDriveFolder.TYPE.equals(this.getType())){
    		icon = R.drawable.ic_folder_blue;
    	}else if(SkyDriveVideo.TYPE.equals(this.getType())){
    		icon = R.drawable.video;
    	}else if(SkyDriveAudio.TYPE.equals(this.getType())){
    		icon = R.drawable.audio;
    	}else if(SkyDriveAlbum.TYPE.equals(this.getType())){
    		icon = R.drawable.ic_folder_blue;
    	}else{
    		icon = getFileIcon(getFileExtesion());
    	}
    	
    	
    	return icon;
    }
    
    public static int getFileIcon(String ext){
    	int icon = R.drawable.default_thumbnail;
    	if("adp".equalsIgnoreCase(ext)){
    		icon = R.drawable.adp;
    	}else if("audio".equalsIgnoreCase(ext)){
    		icon = R.drawable.audio;
    	}else if("docm".equalsIgnoreCase(ext)){
    		icon = R.drawable.docm;
    	}else if("doc".equalsIgnoreCase(ext)){
    		icon = R.drawable.doc;
    	}else if("docx".equalsIgnoreCase(ext)){
    		icon = R.drawable.docx;
    	}else if("dot".equalsIgnoreCase(ext)){
    		icon = R.drawable.dot;
    	}else if("dotm".equalsIgnoreCase(ext)){
    		icon = R.drawable.dotm;
    	}else if("dotx".equalsIgnoreCase(ext)){
    		icon = R.drawable.dotx;
    	}else if("exe".equalsIgnoreCase(ext)){
    		icon = R.drawable.exe;
    	}else if("html".equalsIgnoreCase(ext)){
    		icon = R.drawable.html;
    	}else if("mdb".equalsIgnoreCase(ext)){
    		icon = R.drawable.mdb;
    	}else if("mpp".equalsIgnoreCase(ext)){
    		icon = R.drawable.mpp;
    	}else if("notebook".equalsIgnoreCase(ext)){
    		icon = R.drawable.notebook;
    	}else if("one".equalsIgnoreCase(ext)){
    		icon = R.drawable.one;
    	}else if("onepkg".equalsIgnoreCase(ext)){
    		icon = R.drawable.onepkg;
    	}else if("onetoc2".equalsIgnoreCase(ext)){
    		icon = R.drawable.onetoc2;
    	}else if("pdf".equalsIgnoreCase(ext)){
    		icon = R.drawable.pdf;
    	}else if("photo".equalsIgnoreCase(ext)){
    		icon = R.drawable.photo;
    	}else if("pot".equalsIgnoreCase(ext)){
    		icon = R.drawable.pot;
    	}else if("potm".equalsIgnoreCase(ext)){
    		icon = R.drawable.potm;
    	}else if("potx".equalsIgnoreCase(ext)){
    		icon = R.drawable.potx;
    	}else if("ppa".equalsIgnoreCase(ext)){
    		icon = R.drawable.ppa;
    	}else if("ppam".equalsIgnoreCase(ext)){
    		icon = R.drawable.ppam;
    	}else if("pps".equalsIgnoreCase(ext)){
    		icon = R.drawable.pps;
    	}else if("ppsm".equalsIgnoreCase(ext)){
    		icon = R.drawable.ppsm;
    	}else if("ppsx".equalsIgnoreCase(ext)){
    		icon = R.drawable.ppsx;
    	}else if("ppt".equalsIgnoreCase(ext)){
    		icon = R.drawable.ppt;
    	}else if("pptm".equalsIgnoreCase(ext)){
    		icon = R.drawable.pptm;
    	}else if("pptx".equalsIgnoreCase(ext)){
    		icon = R.drawable.pptx;
    	}else if("pub".equalsIgnoreCase(ext)){
    		icon = R.drawable.pub;
    	}else if("rtf".equalsIgnoreCase(ext)){
    		icon = R.drawable.rtf;
    	}else if("txt".equalsIgnoreCase(ext)){
    		icon = R.drawable.txt;
    	}else if("video".equalsIgnoreCase(ext)){
    		icon = R.drawable.video;
    	}else if("vsd".equalsIgnoreCase(ext)){
    		icon = R.drawable.vsd;
    	}else if("vsdx".equalsIgnoreCase(ext)){
    		icon = R.drawable.vsdx;
    	}else if("xaml".equalsIgnoreCase(ext)){
    		icon = R.drawable.xaml;
    	}else if("xla".equalsIgnoreCase(ext)){
    		icon = R.drawable.xla;
    	}else if("xlam".equalsIgnoreCase(ext)){
    		icon = R.drawable.xlam;
    	}else if("xls".equalsIgnoreCase(ext)){
    		icon = R.drawable.xls;
    	}else if("xlsb".equalsIgnoreCase(ext)){
    		icon = R.drawable.xlsb;
    	}else if("xlsm".equalsIgnoreCase(ext)){
    		icon = R.drawable.xlsm;
    	}else if("xlsx".equalsIgnoreCase(ext)){
    		icon = R.drawable.xlsx;
    	}else if("zip".equalsIgnoreCase(ext)){
    		icon = R.drawable.zip;
    	}else if("xml".equalsIgnoreCase(ext)){
    		icon = R.drawable.xml;
    	}else if("xps".equalsIgnoreCase(ext)){
    		icon = R.drawable.xps;
    	}else if("xsn".equalsIgnoreCase(ext)){
    		icon = R.drawable.xsn;
    	}else if("zip".equalsIgnoreCase(ext)){
    		icon = R.drawable.zip;
    	}else if(Constant.musicFileType.contains(ext.toLowerCase())){
    		icon = R.drawable.audio;
    	}else if(Constant.vedioFileType.contains(ext.toLowerCase())){
    		icon = R.drawable.video;
    	}else if(Constant.pictureFileType.contains(ext.toLowerCase())){
    		icon = R.drawable.photo;
    	}
    	
    	return icon;
    }
    
    private String getFileExtesion(){
    	String ext = "";
    	String fileName = this.getName();
    	int dotIndex=fileName.lastIndexOf('.');
    	if(dotIndex>=0) {
    		ext=fileName.substring(dotIndex+1,fileName.length());
    	}
    	return ext;
    }
    
    public String getDetail(){
    	StringBuffer detail = new StringBuffer();
    	if(mObject.has("size")){
    		long size = mObject.optLong("size");
    		detail.append(readableByteCount(size,true));
    		detail.append(" - ");
    	}
    	detail.append(this.getFileModifyDate());
    	return detail.toString();
    }
    
    private String readableByteCount(long bytes, boolean si) {
        int unit = si ? 1000 : 1024;
        if (bytes < unit) return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
        return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
    }

    public JSONObject toJson() {
        return mObject;
    }
    
    public static String[] getColumns(){
		String[] columns = new String[]{"path","name","type","date","json","meta","parent"};
		return columns;
	}
    
    public boolean isFolder(){
    	return SkyDriveFolder.TYPE.equals(this.getType()) || SkyDriveAlbum.TYPE.equals(this.getType());
    }
    
    public boolean isPhoto(){
    	return SkyDrivePhoto.TYPE.equals(this.getType());
    }
    
    public String getFileModifyDate(){
    	String time = this.getUpdatedTime();
    	
		if(time==null || time.length()==0)
			return "";
		
		String date = time;
		String[] ss1 = time.split("\\+");
		if(ss1.length>1){
			String[] ss2 = ss1[0].split("T");
			if(ss2.length>1){
				date = ss2[0];
				
			}
		}
		
		return date;
	}
    
    public boolean equals(Object obj) {
    	if(obj==null){
    		return false;
    	}
    	
    	if(this == obj){
    		return true;
    	}
    	
    	if(!(obj instanceof SkyDriveObject)){
    		return false;
    	}
    	
    	SkyDriveObject file = (SkyDriveObject) obj;
    	
    	return this.getId().equalsIgnoreCase(file.getId());
    }
}
